package com.itheima.com.Dao;

import com.itheima.com.domain.Student;

public class StudentDao {
    private static Student[] students=new Student[5];
    static {
        Student stu1 = new Student("heima001", "张三", "23", "1999-11-11");
        Student stu2 = new Student("heima002", "李四", "24", "1998-11-11");
        students[0]=stu1;
        students[1]=stu2;
    }
    public static Student[] getAllStudent() {
        return students;
    }

    public static void deleteById(String did) {
        int index = getIndex(did);
        students[index]=null;
    }
public static  int getIndex(String id){
        int index=-1;
    for (int i = 0; i < students.length; i++) {
        Student student=students[i];
        if (student!=null&&student.getId().equals(id)){
            index=i;
            break;
        }
    }
    return index;
}



    public static void addStudent(Student stu) {
        int index=-1;
        for (int i = 0; i < students.length; i++) {
          if (  students[i] ==null){
              index=i;
              break;
          }
        }
        if (index!=-1){
            students[index]=stu;
        }
    }

    public void updateStudent(String uid, Student newStu) {
        int index = getIndex(uid);
        students[index]=newStu;
    }
}
